<?php
class Admin_InfoClassController extends Zend_Controller_Action {
	
     public function init(){
		Zend_Loader::loadClass('AdminModel');
		Zend_Loader::loadClass('TeacherModel');
		Zend_Loader::loadClass('LoginModel');
		Zend_Loader::loadClass('StudentsModel');
		Zend_Loader::loadClass('InstitutesModel');
		Zend_Loader::loadClass('SubjectsModel');
		Zend_Loader::loadClass('InfoClassModel');
	}
	
    function preDispatch(){
    	/*
    	 * Kiem tra dang nhap
    	 */
		
		$auth = Zend_Auth::getInstance();
		if(strcmp($this->_request->getActionName(),'login')!=0){
			if (!$auth->hasIdentity()) {
				$this->_redirect('/admin/index/login');
			}
		}
		/*
		 * Tra cac thong tin ve moduln cho view
		*/
		$this->view->module = $this->_request->getModuleName();
		$this->view->controller =  $this->_request->getControllerName();
		$this->view->action =  $this->_request->getActionName();
		/*
		 * Load template
		*/
		$layoutPath = APPLICATION_PATH  . '/templates/admin/admin';
		$option = array (	'layout' 		=> 'index',
				'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
	}
	
	function importExcelAction(){
		require_once 'file/excel/excel_reader.php';
		
		if($this->_request->isPost()){
		
			/*
			 * Thiết lập kích thước tối đa (10 MB) cho file sẽ upload lên.
			* Chỉ cho phép upload file định dạng xls
			*/
			$maxSize = 1048576;
			$temp = preg_split('/[\/\\\\]+/', $_FILES["file"]["name"]);
			$filename = $temp[count($temp)-1];
		
			// Kiểm tra phần mở rộng của file xem có đúng định dạng hay không.
			if (preg_match('/\.(xls|xlsx)$/i', $filename )) {
			
				if($_FILES["file"]["size"] < $maxSize){
					$dt = new TeacherModel();
					$name_file = $dt->uploadFile();
					//	echo $name_file;die;
					/*
					 * Lấy file excel cần xử lý và require thư viện xử lý file excel.
					* Nhung thu vien xu ly ma nguon mo
					*/
					$filename= "file/excel/".$name_file;
		
		
					/*
					 * Khoi tao doi tuong doc file excel
					*/
					$data = new Spreadsheet_Excel_Reader($filename,true,"UTF-8");
					$rowsnum = $data->rowcount($sheet_index=0); // lay so hang cua sheet
					$colsnum =  $data->colcount($sheet_index=0); // lay so cot cua sheet
					
					/*
					 * Đọc dữ liệu và ghi vào database.
					*/
					$k = 0;
					for ($i=2;$i<=$rowsnum;$i++){
						$max = 0;
						/*
						 * Lay thong tin sv tu file excel
						*/
						$sv["name"] = trim($data->val($i,2));
						$sv["shsv"] = $data->val($i,3);
						$sv["subjects"] = $data->val($i,4);
						$sv["code_subjects"] = $data->val($i,5);

						$info_class_add = array();
					//	$info_class_edit = array();
						$info_class_error = array();

						if($sv["shsv"] && $sv["code_subjects"]){
						//	echo $sv["shsv"];
							$info_class["id_students"] = StudentsModel::getIdStudentByShsv($sv["shsv"]);
							$info_class["id_subjects"] = SubjectsModel::getIdsubjectsByCode($sv["code_subjects"]);
						//	Zend_Debug::dump($info_class);
							if($info_class["id_students"] && $info_class["id_subjects"]){
								$check = InfoClassModel::checkExitsInfoClass($info_class["id_students"],$info_class["id_subjects"]);
						//		Zend_Debug::dump($check);
								if(!$check){
									InfoClassModel::add($info_class);
									$info_class_add[] = $sv;
								}
								else{
									$info_class_edit[] = $sv;
								}
							}
							else{
								$this->view->warning = 3;
							//	break;
								$error = 1;
								$info_class_error[] = $sv;
							}
						}
						else{
							$info_class_error[] = $sv;
						}
					}
					if(!$error) $this->view->success = 1;
				}
				else{
		
					$this->view->warning = 1;
				}
				$this->view->ic_add = $info_class_add;
				$this->view->ic_error = $info_class_error;
				$this->view->ic_edit = $info_class_edit;
			}
			else{
					
				$this->view->warning = 2;
			}
		}
	}
	
	function listStudentClassAction(){
		/*
		 * Lay danh sach lop - sinh vien (co phan trang)
		*/
		
		$page = $this->_request->getParam('page');
		if(!$page) $offset = 1;
		else $offset = $page;
		$this->view->now_page = $offset;
		$this->view->count = count(InfoClassModel::getStudentClass());
		$list = InfoClassModel::getStudentClassLimit(10,($offset-1)*10);
		$this->view->list = $list;
	//	Zend_Debug::dump($list);die;
	}
	
}